package com.guli.product.app;

import java.util.Arrays;
import java.util.Map;

import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;
import com.guli.common.validation.AddValidation;
import com.guli.common.validation.UpdateValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.guli.product.entity.BrandEntity;
import com.guli.product.service.BrandService;


/**
 * 品牌
 * @author zzb
 * @email 1626131069@qq.com
 * @date 2023-10-21 10:18:56
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 展示品牌管理列表
     * @param params
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 修改显示状态
     * @param brandEntity
     * @return
     */
    @PostMapping("/update/status")
    public R updateStatus(@Validated(value = UpdateValidation.class) @RequestBody BrandEntity brandEntity) {
        boolean update = brandService.updateById(brandEntity);
        if (update) {
            return R.ok("状态修改成功");
        }
        return R.error("状态修改失败");
    }

    /**
     * 展示修改操作时该品牌的展示页
     * @param brandId
     * @return
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);
        return R.ok().put("brand", brand);
    }

    /**
     * 保存品牌信息
     * @param brand
     * @return
     */
    @RequestMapping("/save")
    public R save(@Validated(value = AddValidation.class) @RequestBody BrandEntity brand) {
        boolean save = brandService.save(brand);
        if (save) {
            return R.ok();
        }
        return R.error("保存品牌信息失败");
    }

    /**
     * 保存修改后的品牌信息
     * @param brand
     * @return
     */
    @RequestMapping("/update")
    public R update(@Validated(value = UpdateValidation.class) @RequestBody BrandEntity brand) {
        brandService.updateBrandWithRelation(brand);
        return R.ok();
    }

    /**
     * 删除品牌信息
     * @param brandIds
     * @return
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeBrandWithRelation(Arrays.asList(brandIds));
        return R.ok();
    }

}
